package com.bootdo.common.utils.httpUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.ZipOutputStream;

import static com.bootdo.common.utils.encryptionUtil.FileDecryption.DecryptFile;

public class DownLoadFile {

    /**
     * 下载文件
     *
     * @param
     * @return
     */
    public static void main(String[] args) {
        //DownLoadFile.downloadFile("C:\\var\\ENCRY_c22964b220fd4e678955fe029c862975监护人APP测试问题20201214.docx","","","", "","","","", "");
    }

    public static File downloadFile(String filePth, String rootPath, String filename, String filetype, String name, HttpServletResponse response, ZipOutputStream ouputStream, FileInputStream IN, BufferedInputStream bins) {
	    File file =new File(filePth);
        try {
            long seed = System.currentTimeMillis();

            //解密文件
            File decryptedFile = new File(rootPath + File.separator + filename + "." + filetype);
            //调用DecryptFile方法对文件进行解密
            int decryptionFlag = DecryptFile(file, decryptedFile,name,response,ouputStream,IN,bins);

            /**
             * 1：文件正常解密
             * -1：文件没有找到
             * -2：加密文件至少有1字节，源文件少于这个长度
             * -3：源文件没有包含加密时放入的密码，即不是采用本系统进行加密
             * -4：在写入文件时出现的其他异常
             */
            switch (decryptionFlag) {
                case 1:
                    return decryptedFile;
                case -1:
                case -3:
                case -2:
                case -4:
                    return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
